#pragma execution_character_set("utf-8")

#include "devicebutton.h"
#include "qpainter.h"
#include "qevent.h"
#include "qtimer.h"
#include "qdebug.h"

Devicebutton::Devicebutton(QWidget *parent):QWidget(parent)
{
    canMove=false;
    text="1";

    colorNormal="black";
    colorAlarm="red";

    buttonStyle=ButtonStyle_Police;
    buttonColor=ButtonColor_Green;

    isPressed=false;
    lastPoint=QPoint();

    type="police";
    imgPath=":/images/devicebutton/devicebutton";
    imgName=QString("%1_green_%2.png").arg(imgPath).arg(type);

    isDark=false;
    timer=new QTimer(this);
    timer->setInterval(500);
    connect(timer,SIGNAL(timeout()),this,SLOT(checkAlarm()));

    this->installEventFilter(this);
}

Devicebutton::~Devicebutton()
{
    if(timer->isActive())
    {
        timer->stop();
    }
}

void Devicebutton::paintEvent(QPaintEvent *)
{
    double width=this->width();
    double height=this->height();

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    //绘制背景
    QImage img(imgName);
    if(!img.isNull())
    {
        img=img.scaled(width,height,Qt::IgnoreAspectRatio,Qt::SmoothTransformation);
        painter.drawImage(0,0,img);
    }

    //计算字体
    QFont font;
    font.setPixelSize(height*0.37);
    font.setBold(true);

    //自动计算文字绘制区域，绘制防区号
    QRectF rect=this->rect();
    double y=0;
    if(buttonStyle==ButtonStyle_Police)
    {
        y=(30*height/60);
    }
    else if(buttonStyle==ButtonStyle_Bubble)
    {
        y=(8*height/60);
    }
    else if (buttonStyle == ButtonStyle_Bubble2) {
        y = (13 * height / 60);

        font.setPixelSize(width * 0.33);
    } else if (buttonStyle == ButtonStyle_Msg) {
        y = (17 * height / 60);
    } else if (buttonStyle == ButtonStyle_Msg2) {
        y = (17 * height / 60);
    }
    if(y>0)
    {
        rect = QRectF(0, 0, width, height - y);
    }

    //绘制文字标识
    painter.setFont(font);
    painter.setPen(Qt::white);
    painter.drawText(rect,Qt::AlignCenter,text);
}

bool Devicebutton::eventFilter(QObject *watched, QEvent *event)
{

    QMouseEvent *mouseEvent=static_cast<QMouseEvent*>(event);
    if(event->type()==QEvent::MouseButtonPress)
    {
        if(mouseEvent->button()==Qt::LeftButton)
        {
            lastPoint=mouseEvent->pos();
            isPressed=true;
            emit clicked();
            return true;
        }
    }else if(event->type()==QEvent::MouseMove)
    {
        if(canMove&&isPressed)
        {
            int dx=mouseEvent->pos().x()-lastPoint.x();
            int dy=mouseEvent->pos().y()-lastPoint.y();
            this->move(this->x()+dx,this->y()+dy);
            return true;
        }
    }else if(event->type()==QEvent::MouseButtonRelease)
    {
        isPressed=false;
    }else if(event->type()==QEvent::MouseButtonDblClick)
    {
        emit doubleClicked();
    }

    return QWidget::eventFilter(watched,event);
}

void Devicebutton::checkAlarm()
{
    if(isDark)
    {
        imgName=QString("%1_%2_%3.png").arg(imgPath).arg(colorNormal).arg(type);
    }
    else
    {
        imgName=QString("%1_%2_%3.png").arg(imgPath).arg(colorAlarm).arg(type);
    }

    isDark=!isDark;
    this->update();
}

bool Devicebutton::getCanMove() const
{
    return this->canMove;
}

QString Devicebutton::getText() const
{
    return this->text;
}

QString Devicebutton::getColorNormal() const
{
    return this->colorNormal;
}
QString Devicebutton::getColorAlarm() const
{
    return this->colorAlarm;
}

Devicebutton::ButtonStyle Devicebutton::getButtonStyle() const
{
    return this->buttonStyle;
}
Devicebutton::ButtonColor Devicebutton::getButtonColor() const
{
    return this->buttonColor;
}

QSize Devicebutton::sinHint() const
{
    return QSize(50,50);
}
QSize Devicebutton::minimumSizeHint() const
{
    return QSize(10,10);
}

void Devicebutton::setCanMove(bool canMove)          //设置可移动
{
    this->canMove=canMove;
}
void Devicebutton::setText(const QString &text)      //设置显示文字
{
    if(this->text!=text)
    {
        this->text=text;
        this->update();
    }
}

void Devicebutton::setColorNormal(const QString &colorNormal)
{
    if(this->colorNormal!=colorNormal)
    {
        this->colorNormal=colorNormal;
        this->update();
    }
}
void Devicebutton::setColorAlarm(const QString &colorAlarm)
{
    if(this->colorAlarm!=colorAlarm)
    {
        this->colorAlarm=colorAlarm;
        this->update();
    }
}

void Devicebutton::setButtonStyle(const ButtonStyle &buttonStyle)        //设置样式
{
    this->buttonStyle=buttonStyle;
    if(this->buttonStyle==ButtonStyle_Circle)
    {
        this->type="circle";
    }else if(this->buttonStyle==ButtonStyle_Police)
    {
        this->type="police";
    }else if (buttonStyle == ButtonStyle_Bubble) {
        type = "bubble";
    } else if (buttonStyle == ButtonStyle_Bubble2) {
        type = "bubble2";
    } else if (buttonStyle == ButtonStyle_Msg) {
        type = "msg";
    } else if (buttonStyle == ButtonStyle_Msg2) {
        type = "msg2";
    } else {
        type = "circle";
    }
    setButtonColor(buttonColor);
}
void Devicebutton::setButtonColor(const ButtonColor &buttonColor)        //设置颜色
{
    this->buttonColor=buttonColor;
    isDark=false;
    if(timer->isActive())
    {
        timer->stop();
    }

    QString color;
    if(buttonColor==ButtonColor_Green)
    {
        color="green";
    }
    else if (buttonColor == ButtonColor_Blue) {
        color = "blue";
    } else if (buttonColor == ButtonColor_Gray) {
        color = "gray";
    } else if (buttonColor == ButtonColor_Black) {
        color = "black";
    } else if (buttonColor == ButtonColor_Purple) {
        color = "purple";
    } else if (buttonColor == ButtonColor_Yellow) {
        color = "yellow";
    } else if (buttonColor == ButtonColor_Red) {
        color = "red";
    } else {
        color = "green";
    }

    imgName=QString("%1_%2_%3.png").arg(imgPath).arg(color).arg(type);

    if(color==colorAlarm)
    {
        checkAlarm();

        if(!timer->isActive())
        {
            timer->start();
        }
    }

    this->update();
}
